<template>
  <div class="add-edit-dept-view" v-loading="loading">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
      <el-form-item :label="$t('tables_title.simple_name')" prop="simpleName">
        <el-input v-model="ruleForm.simpleName" :placeholder="$t('tables_title.simple_name')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('tables_title.superior_name')" prop="pName">
        <div class="deptname-input" @click="innerVisible = true"></div>
        <el-input v-model="ruleForm.pName" :placeholder="$t('tables_title.superior_name')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('tables_title.full_name')" prop="fullName">
        <el-input v-model="ruleForm.fullName" :placeholder="$t('tables_title.full_name')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('tables_title.remarks')">
        <el-input type="textarea" v-model="ruleForm.description" :placeholder="$t('tables_title.remarks')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('tables_title.sort')" prop="sort">
        <el-input type="number" v-model="ruleForm.sort" :placeholder="$t('tables_title.sort')"></el-input>
      </el-form-item>
      <div style="text-align: center;">
        <el-button type="primary" @click="submitForm('ruleForm')">{{$t('global.submit')}}</el-button>
        <el-button @click="cancelView">{{$t('global.cancel')}}</el-button>
      </div>
    </el-form>

    <el-dialog width="350px" :visible.sync="innerVisible" append-to-body>
        <div class="dept-tree-view">
          <div class="dept-tree-data-view">
            <el-tree :data="treeData" :props="treeProps" node-key="id" default-expand-all :expand-on-click-node="false"
            @node-click="handleNodeClick"></el-tree>
          </div>
          <div style="text-align: center;">
            <!-- <el-button type="primary" @click="innerVisible = false">确定</el-button> -->
            <el-button @click="innerVisible = false">{{$t('global.cancel')}}</el-button>
          </div>
        </div>
    </el-dialog>
  </div>

</template>

<script>

import { addDept,updateDept } from '@/api/deptApi.js'

export default {
  data(){
    return{
      loading:false,
      innerVisible:false,
      treeProps: {
        children: 'children',
        label: 'name'
      },
      ruleForm:{
        simpleName:'',
        pName:'',
        pid:'',
        fullName:'',
        description:'',
        sort:''
      },
      rules:{
          simpleName: [
            { required: true, message: this.$t('dept_management.empty_name'), trigger: 'blur' },
          ],
          pName: [
            { required: true, message: this.$t('dept_management.empty_full_name'), trigger: 'blur' },
          ],
          fullName: [
            { required: true, message: this.$t('dept_management.empty_super_name'), trigger: 'blur' },
          ],
          sort: [
            { required: true, message: this.$t('global.empty_sort'), trigger: 'blur' },
          ],
      }
    }
  },
  props: ["isEdit",'treeData','editDeptInfo'],
  watch:{
    editDeptInfo(newVal,oldVal){
      this.ruleForm = newVal
    },
  },
  components:{

  },
  created(){
    this.ruleForm = this.editDeptInfo
  },
  methods:{
    handleNodeClick(data){      //树形节点点击触发事件
      console.log(data)
      this.ruleForm.pid = data.id
      this.ruleForm.pName = data.name
      this.innerVisible = false
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let formData = new FormData()
          formData.append("simpleName",this.ruleForm.simpleName)
          formData.append("pName",this.ruleForm.pName)
          formData.append("pid",this.ruleForm.pid)
          formData.append("fullName",this.ruleForm.fullName)
          formData.append("description",this.ruleForm.description)
          formData.append("sort",this.ruleForm.sort)
          if(this.ruleForm.deptId){
            formData.append("deptId",this.ruleForm.deptId)
            this.userEditDept(formData)
          }else{
            this.userAddDept(formData)
          }
        } else {
          return false;
        }
      });
    },
    userEditDept(formData){         //修改role信息
      this.loading = true
      updateDept(formData).then((res)=>{
        this.loading = false
        console.log(res)
        if(res.data.code == 200){
          this.$message({
            message: this.$t('global.modified_success'),
            type: 'success'
          });
          this.refreshData()
        }else{
          this.$message.error(res.data.message)
        }
      }).catch((e)=>{

      })
    },
    userAddDept(formData){          //新增管Role
      this.loading = true
      addDept(formData).then((res)=>{
        this.loading = false
        if(res.data.code == 200){
          this.$message({
            message: this.$t('global.added_success'),
            type: 'success'
          });
          this.refreshData()
        }else{
          this.$message.error(res.data.message)
        }
      }).catch((e)=>{

      })
    },
    cancelView(){       //关闭弹窗
      this.$emit('cancelView')
    },
    refreshData(){
      this.$emit('refreshData')
    }
  }
}
</script>

<style lang="scss" scoped>
.add-edit-dept-view{
  padding-right: 30px;
  padding-left: 30px;
}

.dept-tree-view{
  width: 100%;
  height: 550px;
  display: flex;
  flex-direction: column;
  .dept-tree-data-view{
    flex: 1;
    width: 100%;
    margin-top: -10px;
    margin-bottom: 20px;
    overflow: auto;
  }
}
</style>
